"""
主题: 让类支持比较操作
问题: 你想让某个类的实例支持标准的比较运算(比如>=,!=,<=,<等)，但是又不想去实现那一大丢的特殊方法。
提示 : 
"""

from functools import total_ordering


class Room:
    def __init__(self, name, length, width):
        self.name = name
        self.length = length
        self.width = width
        self.square_feet = self.length * self.width

class MasterBedroom(Room):
    def __init__(self, length, width):
        super().__init__("Master Bedroom", length, width)

class LivingRoom(Room):
    def __init__(self, length, width):
        super().__init__("Living Room", length, width)

class Kitchen(Room):
    def __init__(self, length, width):
        super().__init__("Kitchen", length, width)

class Office(Room):
    def __init__(self, length, width):
        super().__init__("Office", length, width)

@total_ordering
class House:
    def __init__(self, name, style):
        self.name = name
        self.style = style
        self.rooms = list()

    @property
    def living_space_footage(self):
        return sum(r.square_feet for r in self.rooms)

    def add_room(self, room):
        self.rooms.append(room)

    def __str__(self):
        return '{}: {} square foot {}'.format(self.name,
                                              self.living_space_footage,
                                              self.style)

    def __eq__(self, other):
        return self.living_space_footage == other.living_space_footage

    def __lt__(self, other):
        return self.living_space_footage < other.living_space_footage

def recipe1():
    # Build a few houses, and add rooms to them
    h1 = House('h1', 'Cape')
    h1.add_room(MasterBedroom(14, 21))
    h1.add_room(LivingRoom(18, 20))
    h1.add_room(Kitchen(12, 16))
    h1.add_room(Office(12, 12))
    h2 = House('h2', 'Ranch')
    h2.add_room(MasterBedroom(14, 21))
    h2.add_room(LivingRoom(18, 20))
    h2.add_room(Kitchen(12, 16))
    h3 = House('h3', 'Split')
    h3.add_room(MasterBedroom(14, 21))
    h3.add_room(LivingRoom(18, 20))
    h3.add_room(Office(12, 16))
    h3.add_room(Kitchen(15, 17))
    houses = [h1, h2, h3]
    print('Is h1 bigger than h2?', h1 > h2)  # prints True
    print('Is h2 smaller than h3?', h2 < h3)  # prints True
    print('Is h2 greater than or equal to h1?', h2 >= h1)  # Prints False
    print('Which one is biggest?', max(houses))  # Prints 'h3: 1101-square-foot Split'
    print('Which is smallest?', min(houses))  # Prints 'h2: 846-square-foot Ranch'


def main():
    print('recipe1'.center(20, '*'))
    recipe1()

if __name__ == '__main__':
    main()                    